
"""版本一GenericAPIView"""

# from meiduo_admin.serializer.usergroup_serializer import *
# from meiduo_admin.pages import MyPage
# from rest_framework.generics import GenericAPIView
# from rest_framework.response import Response
# from meiduo_admin.serializer.permission_serializer import *
#
#
# # 请求方式： GET /meiduo_admin/permission/groups/
# class GroupsView(GenericAPIView):
#     serializer_class = UserGroupSerializer
#     queryset = Group.objects.all()
#     pagination_class = MyPage
#
#     """获取所有用户组"""
#     def get(self, request):
#         groups = self.get_queryset()
#         page = self.paginate_queryset(groups)
#         if page:
#             page_serializer = self.get_serializer(page, many=True)
#             return self.get_paginated_response(page_serializer.data)
#         serializer = UserGroupSerializer(groups, many=True)
#         return Response(serializer.data)
#
#     def post(self, request):
#         group_obj = request.data
#         serializer = UserGroupSerializer(data=group_obj)
#         serializer.is_valid(raise_exception=True)
#         serializer.save()
#         return Response(serializer.data, status=201)
#
#
# # 请求方式： GET /meiduo_admin/permission/simple/
# class GroupView(GenericAPIView):
#     serializer_class = PermissionSerializer
#     queryset = Permission.objects.all()
#
#     """获取权限组"""
#     def get(self, request):
#         groups = self.get_queryset()
#         serializer = PermissionSerializer(groups, many=True)
#         return Response(serializer.data)
#
#
# # 请求方式： GET /meiduo_admin/permission/groups/(?P<pk>\d+)/
# class GroupOnlyView(GenericAPIView):
#     serializer_class = UserGroupSerializer
#     queryset = Group.objects.all()
#
#     """获取单一数据"""
#     def get(self, request, pk):
#         group_obj = self.get_object()
#         serializer = self.get_serializer(group_obj)
#         return Response(serializer.data)
#
#     def put(self, request, pk):
#         group_obj = self.get_object()
#         serializer = UserGroupSerializer(instance=group_obj, data=request.data)
#         serializer.is_valid(raise_exception=True)
#         serializer.save()
#         return Response(serializer.data)
#
#     def delete(self, request, pk):
#         group_obj = self.get_object()
#         group_obj.delete()
#         return Response({})
#


"""版本二ModelViewSet"""

from meiduo_admin.serializer.usergroup_serializer import *
from meiduo_admin.pages import MyPage
from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet
from meiduo_admin.serializer.permission_serializer import *


# 请求方式： GET /meiduo_admin/permission/groups/
class GroupsView(ModelViewSet):
    """获取所有用户组"""
    serializer_class = UserGroupSerializer
    queryset = Group.objects.all()
    pagination_class = MyPage


# 请求方式： GET /meiduo_admin/permission/simple/
class GroupView(ListAPIView):
    """获取权限组"""
    serializer_class = PermissionSerializer
    queryset = Permission.objects.all()
